
const app = getApp()
Page({
  data: {
    address_list: [],
    showChoosePrice: false,
    address: '',
    mall: [],
    mallIdList: [],
    price: 0,
    columns: ["余额支付(免密)", "微信支付"],
    pay: 0,
    successShow: false, // 交易成功弹窗
    confirmShow: false
  },
  onLoad: function (options) {
    this.setData({
      price: options.price,
      mallIdList: options.id,
      index: options.index,
      active: options.active
    })
    for (let i = 0; i < options.id.split(',').length; i++) {
      this.getMallDetail(options.id.split(',')[i])
    }
    wx.removeStorageSync('addressIndex');
    this.getAddressList()
  },
  onShow() {
    let _this = this;
    if (wx.getStorageSync('addressIndex') !== '') {
      _this.setData({
        address: _this.data.address_list[wx.getStorageSync('addressIndex')]
      })
    } else {
      this.getDefaultAdd()
    }
  },
  getDefaultAdd() {
    let _this = this
    app.util.request({
      url: "api/terminus/findAcquiescentByOpenId",
      method: "get",
      success(response) {
        if (response.data.code == 0) {
          _this.setData({
            address: response.data.data
          })
        } else if (response.data.code == 501) {
          wx.showToast({
            icon: 'none',
            title: '未设置默认地址',
          })
        }
      }
    })
  },
  getMallDetail(id) {
    let _this = this
    app.util.request({
      url: 'api/award/findById',
      method: 'post',
      data: {
        id: id
      },
      success(response) {
        if (response.data.code == 0) {
          let mall = _this.data.mall
          mall.push(response.data.data)
          _this.setData({
            mall: mall
          })
        }
      }
    })
  },
  showChoosePrice() {
    if (this.data.address == '') {
      wx.showToast({
        title: '请选择收货地址',
        icon: 'none'
      })
    } else {
      if (this.data.price == 0) {
        this.toSend()
      } else {
        this.setData({
          showChoosePrice: true
        })
      }
    }
  },
  onClose() {
    this.setData({
      showChoosePrice: false
    })
  },
  onConfirm(e) {
    this.setData({
      pay: e.detail.index
    })
  },
  toSend() {
    let _this = this;
    let id = _this.data.mallIdList.toString()
    app.util.request({
      url: 'api/award/shipments',
      method: 'post',
      data: {
        id: id,
        recipients: _this.data.address.recipients,
        phone: _this.data.address.phone,
        address: _this.data.address.address,
        pay: _this.data.pay
      },
      success(response) {
        if (response.data.code == 200 || response.data.code == 0) {
          if (_this.data.pay === 1) {
            _this.payorder(response.data.data)
          } else {
            _this.setData({
              successShow: true
            })
          }
        } else {
          wx.showToast({
            title: response.data.message,
            icon: 'none'
          })
        }
      }
    })
  },
  payorder(outTradeNo) {
    let _this = this;
    _this.setData({
      loading: true
    })
    let data = {
      outTradeNo: outTradeNo
    };
    app.util.request({
      url: "wxPay/miniAppPay",
      data: data,
      method: "get",
      success(response) {
        let data = response.data.data;
        wx.requestPayment({
          timeStamp: data.timeStamp,
          nonceStr: data.nonceStr,
          package: data.package,
          signType: data.signType,
          paySign: data.paySign,
          appId: data.appId,
          success(res) {
            if (res.errMsg == "requestPayment:ok") {
              wx.showToast({
                title: '支付成功！',
              })
              _this.setData({
                hasPay: true
              })
              setTimeout(() => {
                wx.navigateTo({
                  url: '/pages/goods/my-goods-index?active=' + _this.data.active,
                })
              }, 2000)
            }
          },
          fail(res) {
            console.log(res)
            _this.setData({
              hasPay: false
            })
          }
        })
      },
      complete() {
        _this.setData({
          loading: false
        })
      }
    })
  },
  weixinPay(tradeNo) {
    let _this = this
    app.util.request({
      url: 'wxPay/miniAppPay',
      method: "post",
      data: {
        outTradeNo: tradeNo
      },
      success(response) {
        if (response.data.code == 0) {
          setTimeout(() => {
            wx.navigateTo({
              url: '/pages/goods/my-goods-index',
            })
          }, 1500)
        }
      }
    })
  },
  onBack() {
    wx.navigateBack({
      delta: 1,
    })
  },
  changeAddress() {
    wx.navigateTo({
      url: '/pages/address/address-index?choose=true',
    })
  },
  getAddressList() {
    let _this = this;
    app.util.request({
      url: "api/terminus/findall",
      method: "get",
      success(response) {
        if (response.data.code == 0) {
          _this.setData({
            address_list: response.data.data
          })
        } else {
          _this.setData({
            address_list: []
          })
        }
      }
    })
  },
})
